#define RPAK_IMPLEMENTATION
#include "rpak.h"

int main(void)
{
	// 最大值：2 - 32,767，4 - 2,147,483,647（2G）
	//printf("sizeof(int) = %lu\n", sizeof(int));

	/* debug: GetPrevDirectoryPath, GetDirectoryPath
	const char *tfile = "data/test/1.txt";
	const char *pdir = GetPrevDirectoryPath(tfile); // data/test
	printf("GetPrevDirectoryPath(%s) = %s\n", tfile, pdir);
	const char *ddir = GetDirectoryPath(tfile); // ./data/test
	printf("GetDirectoryPath(%s) = %s\n", tfile, ddir); */

	/* debug: LoadDirectoryFilesEx - 以下设置读取全部文件夹
	FilePathList dirs = LoadDirectoryFilesEx(argv[1], "DIR", true);
	printf("dirs: %d\n", dirs.count);
	for (int  i = 0; i < dirs.count; i++)
	{
	    printf("%s\n", dirs.paths[i]);
	}
	UnloadDirectoryFiles(dirs); */

	/* debug: rpakPackDir
	rpakPackDir("res", "data/res"); */

	/* debug: encrypt, decrypt
	unsigned char str[] = "Hello World!";
	unsigned int strLen = strlen((char *)str);
	printf("original text: %s\n", str);
	rpakXcrypt(str, strLen);
	printf("encrypted: %s\n", str);
	rpakXcrypt(str, strLen);
	printf("decrypted: %s\n", str); */

	int scrW = 384, scrH = 512;

	InitWindow(scrW, scrH, "rpak demo");
	InitAudioDevice();

	SetTargetFPS(60);

	Sound sound = { 0 };
	int fontSize = 28;
	Font font = { 0 };
	char *text;
	Texture2D texture = { 0 };
	Model model = { 0 };
	int animCount = 0;
	ModelAnimation *anims = NULL;

	/* debug: rpakLoadChunk
	rpakChunk chunk = rpakLoadChunk("res/nfo.txt", "data/res");
	if (chunk.data != NULL)
	{
	    printf("file name: %s\n", chunk.info.fileName);
	    printf("file type: %d\n", chunk.info.fileType);
	    printf("content: %s\n", chunk.data);
	}
	rpakUnloadChunk(chunk); */

	/* debug: rpakLoadChunks */
	unsigned int chunkCount;
	rpakChunk *chunks = rpakLoadChunks("data/res", &chunkCount);
	for (int i = 0; i < chunkCount; i++)
	{
		const char *fileExt = GetFileExtension((char *)chunks[i].info.fileName);
		switch (chunks[i].info.fileType) // 注：这里每种只有一个数据
		{
			case rpakFileType_Text:
				text = (char *)chunks[i].data;
				break;
			case rpakFileType_Image:
			{
				Image img = LoadImageFromMemory(fileExt,
				                                chunks[i].data,
				                                chunks[i].info.baseSize);
				texture = LoadTextureFromImage(img);
				UnloadImage(img);
				rpakUnloadChunk(chunks[i]);
				break;
			}
			case rpakFileType_Audio:
			{
				Wave wave = LoadWaveFromMemory(fileExt,
				                               chunks[i].data,
				                               chunks[i].info.baseSize);
				sound = LoadSoundFromWave(wave);
				UnloadWave(wave);
				rpakUnloadChunk(chunks[i]);
				break;
			}
			case rpakFileType_Font:
				font = LoadFontFromMemory(fileExt,
				                          chunks[i].data,
				                          chunks[i].info.baseSize,
				                          fontSize, NULL, 0);
				rpakUnloadChunk(chunks[i]);
				break;
			case rpakFileType_Model:
				model = LoadRpakModelByData(fileExt,
				                            chunks[i].data,
				                            chunks[i].info.baseSize);
				anims = LoadRpakModelAnimationsByData(fileExt,
				                                      chunks[i].data,
				                                      chunks[i].info.baseSize,
				                                      &animCount);
				rpakUnloadChunk(chunks[i]);
				break;
			case rpakFileType_None:
				break;
		}
	}
	//rpakUnpackChunks(chunks, count, "data1");

	// |/ 45, |- 90, ...
	Camera camera =
	{
		.position = (Vector3) { 0, 0, 0.5f },
		.target = Vector3Zero(),
		.up = (Vector3) { 0, 1.f, 0 },
		.fovy = 90.f,
		.projection = CAMERA_PERSPECTIVE
	};

	int animIdx = 7; // idle
	int animFrame = 0; // Current animation frame

	while (!WindowShouldClose())
	{
		if (IsKeyPressed(KEY_SPACE))
			PlaySound(sound);

		if (animCount > 0)
		{
			// Update animation frame
			animFrame = (animFrame + 1) % anims[animIdx].frameCount;
			UpdateModelAnimation(model, anims[animIdx], animFrame);
		}

		BeginDrawing();
		{
			ClearBackground(DARKGRAY);

			BeginMode3D(camera);
			{
				//DrawGrid(10, 1.f);
				DrawBillboard(camera, texture, (Vector3) { 0, 0, -0.5f }, 2.f, WHITE);
				DrawModel(model, (Vector3) { -0.175f, -0.25f, 0 }, 0.25f, WHITE);
			}
			EndMode3D();

			DrawTextEx(font, text, (Vector2) { 6, scrH - fontSize }, (float)fontSize, 0, RED);

#ifdef _DEBUG
			DrawFPS(2, 0);
#endif // _DEBUG
		}
		EndDrawing();
	}

	if (animCount > 0)
		UnloadModelAnimations(anims, animCount);
	UnloadModel(model);
	UnloadTexture(texture);
	UnloadFont(font);
	UnloadSound(sound);

	CloseAudioDevice();
	CloseWindow();

	return 0;
}
